from ability.models import *
exam = Exam.objects.get(id=6)
course_id = Course.objects.get(id=exam.course.id).id
for user in UserProfile.objects.filter(user_type='S', student__isnull=False, student__course__id=course_id):
    student = user.student
    if StudentCourse.objects.get(student__id=student.id, course__id=course_id).status == 'N':
        paper = Paper()
        paper.exam = exam
        paper.student = student
        paper.save()
        for eq in exam.examquestion_set.all():
            paperquestion = PaperQuestion()
            paperquestion.paper = paper
            paperquestion.question = eq
            paperquestion.save()
            if eq.question.question_type in ['MA', 'MC', 'TF']:
                paperquestion.choice_answer.add(eq.question.choicedetail.optiondetail_set.order_by('?')[0])
            else:
                paperquestion.blank_answer = "ashgjfhdsfgsdfkshdfuiasgfjskdfgjdsfgjsd"
            paperquestion.save()
